<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**
     * 后台分类添加
     */
    public function getAdd()
    {
        //获取格式化数据
        $cates = self::getCates();
        //显示添加页面
        return view('cate.add',['cates'=>$cates]);
    }

    /**
     * 后台分类添加
     */
    public function postInsert(Request $request)
    {
       //拼接数据
        $data = $request->only(['pid','name']);
        //判断是否为顶级分类
        if($data['pid'] == 0){
            $data['path'] = 0;
        }else{
            //查询父级的path路径
            $res = DB::table('cates')->where('id',$data['pid'])->first();
            $data['path'] = $res->path.','.$data['pid'];
        }
        if(empty($data['name'])){
            return back()->with('error','分类名称不能为空');
        }
       //执行数据插入
        $res = DB::table('cates')->insert($data);
        if($res){
            return redirect('/admin/cate/index')->with('success','添加成功');
        }else{
            return back()->with('error','数据添加失败');
        }
    }

    public function getIndex(Request $request)
    {
        
        //查询数据
        //框架中的方法
        // $cates = DB::table('cates')
        //     ->select(DB::raw('*,concat(path,",",id) as paths'))
        //     ->orderBy('paths')
        //     ->get();
        //获取格式化数据
        $cates = self::getCates();
        $num = $request->input('num',10);
        //查询所有数据分页显示 判断
        if($request->input('keywords')){
            $users = DB::table('cates')->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $users = DB::table('cates')->paginate($num);
        }
        //解析模板
        return view('cate.index',['cates'=>$cates,'users'=>$users,'request'=>$request->all()]);
    }

    /**
     * 按照要求 格式化 数据分类
     */
    public static function getCates()
    {
        $cates = DB::select('select *,concat(path,",",id) as paths from cates order by paths');
        foreach($cates as $k => $v){
            //获取长度
            $arr = explode(',',$v->path);
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|----',$len).$v->name;
        }
        return $cates;
    }

    /**
     * 删除操作
     */
    public function getDelete(Request $request)
    {
        //获取id
        $id = $request->input('id');
        $res = DB::table('cates')->where('pid',$id)->count();
        if($res > 0){
             return back()->with('error','该类下有子分类,不允许删除');
        }
        $res = DB::table('cates')->where('id',$id)->delete();
         if($res){
            return redirect('/admin/cate/index')->with('success','删除成功');
        }else{
            return back()->with('error','数据删除失败');
        }
    }

    /**
     * 修改分类信息
     */
    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        //通过id查询
        $cates = DB::table('cates')->where('id',$id)->first();
        // //解析模板  分配页面
        return view('cate.edit',['cates'=>$cates]);
    }

    /**
     * 修改数据
     */
    public function postUpdate(Request $request)
    {
        $data = $request->except(['_token','id']);
        $id = $request->input('id');
        $res = DB::table('cates')->where('id',$id)->update($data);
        // dd($res);
        if($res){
            return redirect('/admin/cate/index')->with('success','修改成功');
        }else{
            return  back()->with('error','修改失败');
           
        }

    }

}
